package com.ruoyi.mch.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.ruoyi.mch.bo.WxMchConfigAddBo;
import com.ruoyi.mch.bo.WxMchConfigQueryBo;
import com.ruoyi.mch.bo.WxMchConfigEditBo;
import com.ruoyi.mch.domain.WxMchConfig;
import com.ruoyi.mch.mapper.WxMchConfigMapper;
import com.ruoyi.mch.vo.WxMchConfigVo;
import com.ruoyi.mch.service.IWxMchConfigService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 微信商户配置Service业务层处理
 *
 * @author ruoyi
 * @date 2021-06-02
 */
@Service
public class WxMchConfigServiceImpl extends ServiceImpl<WxMchConfigMapper, WxMchConfig> implements IWxMchConfigService {

    @Override
    public WxMchConfigVo queryById(Long configId){
        WxMchConfig db = this.baseMapper.selectById(configId);
        return BeanUtil.toBean(db, WxMchConfigVo.class);
    }

    @Override
    public List<WxMchConfigVo> queryList(WxMchConfigQueryBo bo) {
        LambdaQueryWrapper<WxMchConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getAppid()), WxMchConfig::getAppid, bo.getAppid());
        lqw.eq(StrUtil.isNotBlank(bo.getAppKey()), WxMchConfig::getAppKey, bo.getAppKey());
        lqw.eq(StrUtil.isNotBlank(bo.getMchid()), WxMchConfig::getMchid, bo.getMchid());
        lqw.eq(StrUtil.isNotBlank(bo.getMchKey()), WxMchConfig::getMchKey, bo.getMchKey());
        lqw.like(StrUtil.isNotBlank(bo.getMchName()), WxMchConfig::getMchName, bo.getMchName());
        return entity2Vo(this.list(lqw));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<WxMchConfigVo> entity2Vo(Collection<WxMchConfig> collection) {
        List<WxMchConfigVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, WxMchConfigVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<WxMchConfig> page = (Page<WxMchConfig>)collection;
            Page<WxMchConfigVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(WxMchConfigAddBo bo) {
        WxMchConfig add = BeanUtil.toBean(bo, WxMchConfig.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(WxMchConfigEditBo bo) {
        WxMchConfig update = BeanUtil.toBean(bo, WxMchConfig.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(WxMchConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
